<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>

                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 使用el-tree组件展示主题内容 -->
        <el-tree
          default-expand-all
          :data="list"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEditor(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 新增子部门或者编辑部门弹层 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEditor?'编辑部门':'新增子部门'"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <addOrEdit
          :id="curid"
          ref="myAddRef"
          :is-editor="isEditor"
          :origin-list="originList"
          @close="showDialog=false"
          @success="hSuccess"
        />
      </el-dialog>

    </div>
  </div>
</template>
<script>
import addOrEdit from './deptDialog.vue' // 添加部门或者编辑部门 弹层
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'

export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [], // 保存部门列表
      showDialog: false, // 控制弹层的显示和隐藏
      curid: '', // 当前点击得部门id
      isEditor: false, // false 新增 || true 编辑
      originList: []
    }
  },
  watch: { // 解决数据不更新方式
    id: function(new_value, old_value) {
      this.loadDepartments()
    }
  },
  created() {
    this.loadDepartments() // 重新获取部门列表
  },
  methods: {
    hClose() {
      // 重置表单 清空校验信息
      this.$refs.myAddRef.resetForm()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('你确定是否要删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hSuccess() {
      this.loadDepartments() // 重新渲染页面
      this.showDialog = false // 关闭弹层
    },
    // 点击编辑
    hEditor(id) {
      this.curid = id
      this.isEditor = true // 编辑状态
      this.$nextTick(() => {
        this.$refs.myAddRef.loadDepartmentsDetail()
      })
      this.showDialog = true
    },
    // 点击新增部门
    hAdd(id) {
      this.curid = id
      this.showDialog = true
      this.isEditor = false
    },
    // 加载部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        // console.log('getDepartments', res)
        res.data.depts.shift()// 删除第一条数据 公司名称
        // this.list = res.data.depts
        this.list = transDataToTreeData(res.data.depts)
        // console.log('this.list', this.list)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
